package com.yoo.ons.dialog.purchase.member

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yoo.module_base.R
import com.yoo.module_base.base.BaseDialogFragment
import com.yoo.ons.adapter.PurchaseMemberBannerAdapter
import com.yoo.ons.adapter.PurchaseMemberDescAdapter
import com.yoo.ons.adapter.PurchaseMemberPriceAdapter
import com.yoo.ons.databinding.DialogFragmentPurchaseMemberBinding
import com.yoo.ons.model.PurchaseMemberModel

/**
 * PurchaseMemberDialogFragment
 */
class PurchaseMemberDialogFragment : BaseDialogFragment() {
    private var innerBinding: DialogFragmentPurchaseMemberBinding? = null
    private val binding get() = innerBinding!!
    private val adapterPrice by lazy { PurchaseMemberPriceAdapter() }
    private val adapterDesc by lazy { PurchaseMemberDescAdapter() }

    override fun setWindow(win: Window) {
        win.setWindowAnimations(R.style.DialogAnimation)
        win.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        win.attributes = lp
        win.setGravity(Gravity.BOTTOM)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding = innerBinding
            ?: DialogFragmentPurchaseMemberBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
    }

    private fun initData() {
        initBannerData()
        initMemberData()
        initMemberDesc()
    }

    private fun initBannerData() {
        val context = activity ?: return
        val list = mutableListOf<PurchaseMemberModel>().apply {
            add(
                PurchaseMemberModel().copy(
                    img = "https://img0.baidu.com/it/u=3947399543,189778008&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
                    title = "Unlimited Matches, Likes,Chats1",
                    subTitle = "With unlimited daily swipes, won’t miss every change to meet your crush!1"
                )
            )
            add(
                PurchaseMemberModel().copy(
                    img = "https://img0.baidu.com/it/u=3947399543,189778008&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
                    title = "Unlimited Matches, Likes,Chats2",
                    subTitle = "With unlimited daily swipes, won’t miss every change to meet your crush!2"
                )
            )
            add(
                PurchaseMemberModel().copy(
                    img = "https://img0.baidu.com/it/u=3947399543,189778008&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
                    title = "Unlimited Matches, Likes,Chats3",
                    subTitle = "With unlimited daily swipes, won’t miss every change to meet your crush!3"
                )
            )
        }
        binding.bannerPurchaseMember.setAdapter(PurchaseMemberBannerAdapter(context, list))
    }

    private fun initMemberData() {
        binding.rvPurchaseMemberType.itemAnimator = null
        binding.rvPurchaseMemberType.layoutManager = LinearLayoutManager(
            binding.rvPurchaseMemberType.context,
            RecyclerView.HORIZONTAL,
            false
        )
        binding.rvPurchaseMemberType.adapter = adapterPrice
        adapterPrice.set(mutableListOf<String>().apply {
            add("")
            add("")
            add("")
        })
    }

    private fun initMemberDesc(){
        binding.rvPurchaseMemberContent.itemAnimator = null
        binding.rvPurchaseMemberContent.layoutManager = LinearLayoutManager(
            binding.rvPurchaseMemberContent.context,
            RecyclerView.VERTICAL,
            false
        )
        binding.rvPurchaseMemberContent.adapter = adapterDesc
        adapterDesc.set(mutableListOf<String>().apply {
            add("")
            add("")
            add("")
        })
    }

    override fun onDestroyView() {
        super.onDestroyView()
        innerBinding = null
    }

    companion object {
        fun newInstance(): PurchaseMemberDialogFragment {
            return PurchaseMemberDialogFragment()
        }
    }
}